
************************************************************************************************
*** REPLICATION FILES OF TABLES AND FIGURES 
*** Paper: Social incentives, delivery agents and the effectiveness of development interventions 
************************************************************************************************

clear all
set more off
set mem 200m
set maxvar 30000

cap ssc install rwolf
cap ssc install randcmd
cap ssc install shapley2

** ADJUST DIRECTORY
global user ""

cd "$user"				

global controls "branch1 branch2 branch3"
global stratification "stratification_var_pop stratification_var_prop_farmers stratification_var_mktdistance"

*****************
* 1. MAIN TABLES
*****************

*~*~* Table 1: Balance on Household Characteristics

use "SocialIncentives_Data_TandC" , clear
set seed 2222

local HH "primary acres_owned wealth_index consumption_food2se"
local Seeds "know_seeds belief_seeds adopt_seeds_ever nb_techniques_known nb_techniques_belief adopt_tech_ever  adopt_mixed_cropping_ever"
local Agri " hours_agri_day acres_cultivated  nb_cropsN nb_mktcrops   share_output_sold  netoutput2se"

local depvar `HH' `Seeds' `Agri'

count if agri==0
local count1=r(N)
count if agri==1 
local count2=r(N)

foreach x of local depvar {

sum `x'_B  if agri==0, d
if abs(r(mean))>=1000 {
local m1`x': di %12.0fc r(mean)
}
if  abs(r(mean))>=10 & abs(r(mean))<1000 {
local m1`x': di %12.1fc r(mean)
}
if abs(r(mean))>=10 & abs(r(mean))<100 {
local m1`x': di %12.2fc r(mean)
}
if abs(r(mean))<10 {
local m1`x': di %12.3fc r(mean)
}

if abs(r(sd))>=1000 {
local sd1`x': di %12.0fc r(sd)
}
if  abs(r(sd))>=10 & abs(r(sd))<1000 {
local sd1`x': di %12.1fc r(sd)
}
if abs(r(sd))>=10 & abs(r(sd))<100 {
local sd1`x': di %12.2fc r(sd)
}
if abs(r(sd))<10 {
local sd1`x': di %12.3fc r(sd)
}

sum `x'_B  if agri==1, d
if abs(r(mean))>=1000 {
local m2`x': di %12.0fc r(mean)
}
if  abs(r(mean))>=10 & abs(r(mean))<1000 {
local m2`x': di %12.1fc r(mean)
}
if abs(r(mean))>=10 & abs(r(mean))<100 {
local m2`x': di %12.2fc r(mean)
}
if abs(r(mean))<10 {
local m2`x': di %12.3fc r(mean)
}

if abs(r(sd))>=1000 {
local sd2`x': di %12.0fc r(sd)
}
if  abs(r(sd))>=10 & abs(r(sd))<1000 {
local sd2`x': di %12.1fc r(sd)
}
if abs(r(sd))>=10 & abs(r(sd))<100 {
local sd2`x': di %12.2fc r(sd)
}
if abs(r(sd))<10 {
local sd2`x': di %12.3fc r(sd)
}
reg `x'_B agri  $controls , r cluster(clus_unique)
test agri
local p12`x': di %12.3fc  r(p)
}

display "counts, `count1', `count2'"
foreach x of local depvar {
display "`x'" "," `m1`x'' "," `m2`x'' ","    `p12`x'' 
display "`x'" "," `sd1`x'' ","  `sd2`x'' ","    
}

*~*~* Table 2: Aggregate Impacts 

local filename = "table"
capture erase "`filename'.txt"
capture erase "`filename'.xml"

use "SocialIncentives_Data_TandC", clear
set seed 2222

local rep=500

g treated_B=0

local depvar1 " treated adopt_seedsY_MF trained_MF  adopt_seedsY_BRACnotMF adopt_seedsY_notBRAC mean_obs_tech "

** MHT
local k=0
foreach x of local depvar1{
local k=`k'+1
gen var`k'=`x'_F
gen var`k'_B=`x'_B
}

preserve
keep var* $controls agri clus_unique
rwolf var1 var2 var3 var4 var5 var6, ///
indepvar(agri)  controls($controls var1_B var2_B var3_B var4_B var5_B)  ///
reps(`rep') vce(cluster clus_unique) seed(2222) cluster(clus_unique)
restore

local k=0
foreach x of varlist  var1 var2 var3 var4 var5 var6{
local k=`k'+1
/*pv*/ local pv_mht`k': di %12.3fc e(rw_`x')
}

drop var*
local k=0
foreach x of local depvar1{
local k=`k'+1
cap sum `x'_B
cap gen `x'_B=0

if r(N)==0 {
replace `x'_B=0 if `x'_B==.
}
preserve
keep `x'_F agri  $controls  clus_unique `x'_B
randcmd ((agri) reg `x'_F agri  $controls `x'_B,  cluster(clus_unique)), treatvars(agri) reps(`rep') seed(2222)
restore
matrix r=e(RCoef)
local p_ri:  di %12.3fc  r[1,3]

xi: reg  `x'_F agri  $controls `x'_B, r cl(clus_unique)
local obs=e(N)
local nana=0.111
sum `x'_F if agri==0
local m`x'=r(mean)
outreg2 using `filename'.xls, keep(agri)  bdec(3) sdec(3) label ctitle(`x')  addstat( "p-value RI" ,   `p_ri' ,  "p-value MHT" , `pv_mht`k'' , "mean control", `m`x'', "observations", `obs') nocons nor2 noobs  adec(3) afmt(f)
}


local rep=500
replace consumption_food2se_F=consumption_food2se_F*(12/52)

** MHT
local depvar1 " netoutput2se nb_mktcrops  consumption_food2se total_cons total_value_agri_assets2se"

local k=0
foreach x of local depvar1{
local k=`k'+1
gen var`k'=`x'_F
cap sum `x'_B
cap gen `x'_B=0
if r(N)==0 {
replace `x'_B=0 if `x'_B==.
}
gen var`k'_B=`x'_B
}

preserve
keep var* $controls agri clus_unique
rwolf var1 var2, ///
indepvar(agri)  controls($controls var1_B var2_B) ///
reps(`rep') vce(cluster clus_unique) seed(2222) cluster(clus_unique)
restore
local k=0
foreach x of varlist var1 var2 {
local k=`k'+1
/*pv*/ local pv_mht`k': di %12.3fc e(rw_`x')
}

preserve
keep var* $controls agri clus_unique
rwolf var3 var4 var5, ///
indepvar(agri)  controls($controls var3_B var4_B var5_B) ///
reps(`rep') vce(cluster clus_unique) seed(2222) cluster(clus_unique)
restore
foreach x of varlist  var3 var4 var5 {
local k=`k'+1
/*pv*/ local pv_mht`k': di %12.3fc e(rw_`x')
}


drop var*
local k=0
foreach x of local depvar1{
local k=`k'+1
preserve
keep `x'_F agri  $controls clus_unique `x'_B
randcmd ((agri) reg `x'_F agri  $controls `x'_B,  cluster(clus_unique)), treatvars(agri) reps(`rep') seed(2222)
restore
matrix r=e(RCoef)
local p_ri:  di %12.3fc  r[1,3]

xi: reg  `x'_F agri  $controls `x'_B, r cl(clus_unique)
local obs=e(N)
sum `x'_F if agri==0
local m`x'=r(mean)
local k=`k'
outreg2 agri using `filename'.xls,  keep(agri) bdec(3) sdec(3) label ctitle(`x') addstat("p-value RI", `p_ri', "p-value MHT", `pv_mht`k'', "mean control", `m`x'', "observations", `obs') nocons nor2 noobs adec(3) afmt(f)
}


*~*~* Table 3: Balance on Social Ties to Actual and Counterfactual Delivery Agents

*** FIRST SET OF COLUMNS

use "SocialIncentives_Data_Tonly" , clear
set seed 2222

drop adopt_seeds_B
rename adopt_seeds_ever_B adopt_seeds_B

local depvar "primary acres_owned adopt_seeds adopt_tech_ever hours_agri_day acres_cultivated netoutput2se_pa "

foreach x of local depvar{
bysort agri_unique: sum `x'_B,d
forvalues i=5(5)95{
local `x'`i'=r(p`i')
}
gen `x'Pctile=5 if `x'_W_B<``x'5'
forvalues i=5(5)95{
replace `x'Pctile=`i' if `x'_W_B<``x'`i''
}
replace `x'Pctile=100 if `x'_W_B>``x'95'&`x'_W_B!=.
}

collapse netoutput2se* age* read* primary* nb_HHmem* acres_owned* hours_agri* total_nb_assets* adopt_seeds* adopt_tech_ever* tech1*  acres_cultivated* nb_mktcrops* output* engaged* netoutput_pa2se* cluster* branch1-branch4 strat*, by(agri_unique)

foreach y in depvar{
foreach x of local `y' {

qui egen m`x'=mean(`x'_W_B) 
local m`x' : di %4.3f m`x'
qui egen sd`x'=sd(`x'_W_B) 
local sd`x': di %4.3f sd`x'

qui egen m2`x'=mean(`x'_L_B) 
local m2`x': di %4.3f m2`x'
qui egen sd2`x'=sd(`x'_L_B) 
local sd2`x': di %4.3f sd2`x'

qui egen m3`x'=mean(`x'Pctile) 
local m3`x' : di %4.0f m3`x'
qui egen sd3`x'=sd(`x'Pctile) 
local sd3`x': di %4.0f sd3`x'

qui ttest `x'_L_B==`x'_W_B
local p1: di %4.3f r(p)

display "`x'" "," `m`x'' "," `m2`x'' ","  `m3`x'' "," `p1'
display "`x'" "," `sd`x'' ","  `sd2`x'' "," 
}
}


*** SECOND SET OF COLUMNS

use "SocialIncentives_Data_Tonly" , clear
set seed 2222

local HH "primary acres_owned   wealth_index consumption_food2se consQ1    "
local Agri " adopt_seeds_ever  adopt_tech_ever   hours_agri_day acres_cultivated   netoutput2se "
local Dist "distance distancenotMF samevillMF samevillnotMF "
local depvar `HH' `Agri' `Dist'


foreach x of local depvar {

foreach t in  know {

qui gen `x'_`t'_W_1=`x'_B if  `t'_onlyMF_B==1
qui gen `x'_`t'_L_1=`x'_B if `t'_onlynotMF_B==1
qui gen `x'_`t'_B_1=`x'_B if `t'_both_B==1
qui gen `x'_`t'_N_1=`x'_B if `t'_none_B==1

foreach k in W L B N{

egen m_`t'_`k'_1=mean(`x'_`t'_`k'_1)
local m_`t'_`k'_1 : di %4.3f  m_`t'_`k'_1
egen sd_`t'_`k'_1= sd(`x'_`t'_`k'_1)
local sd_`t'_`k'_1 : di %4.3f sd_`t'_`k'_1

}


qui reg `x'_B `t'_onlyMF_B `t'_onlynotMF_B `t'_both_B $controls, cluster(cluster`t')
qui test `t'_onlyMF_B=`t'_onlynotMF_B
local p1_`t': di %4.3f r(p)
qui test `t'_onlyMF_B=`t'_both_B
local p2_`t': di %4.3f r(p)
qui test `t'_onlyMF_B=0
local p3_`t': di %4.3f r(p)

display "`x'_B" "," `m_`t'_W_1' ","  `m_`t'_L_1' "," "," `m_`t'_B_1' ","  `m_`t'_N_1'  "," `p1_`t'' "," `p2_`t'' "," `p3_`t''
display "`x'_B" "," `sd_`t'_W_1' ","   `sd_`t'_L_1' ","    "," `sd_`t'_B_1' ","   `sd_`t'_N_1' ","
drop  m_* sd_*

}
}

*~*~* Table 4: Coverage

local filename = "Coverage"
capture erase "`filename'.txt"
capture erase "`filename'.xml"

use "SocialIncentives_Data_Tonly" , clear  
set seed 2222

gen treated_exc_F=(treated_F==1 & (know_onlyMF_B==1 | know_onlynotMF_B==1))  
replace treated_exc_F=. if treated_F==. & (know_onlyMF_B==. | know_onlynotMF_B==.)

collapse (mean) same* diff*  $controls  (sum)  know*  treated_F treated_exc_F, by(agri_unique)

rename know_onlyMF_B know_onlyMF
rename know_onlynotMF_B know_onlynotMF

gen know_excl=(know_onlyMF+know_onlynotMF)
gen know_onlyMF2=know_onlyMF*know_onlyMF

foreach x in  treated_exc_F {

** COLUMN (1)

** partial R2
pcorr `x'  know_onlyMF know_excl   $controls
matrix P=r(p_corr)
local P2=P[1,1]^2

xi: reg `x'  know_onlyMF know_excl  $controls  , robust
est store reg1
local obs=e(N)
local r2=e(r2)


shapley2, stat(r2) 
matrix S=e(shapley_rel)
local S=S[1,1]
sum `x' 
local mean=r(mean)
outreg2 using  `filename'.xls, append keep( know_onlyMF ) bdec(3) sdec(3) nor2  addstat(mean, `mean', R2, `r2', partial R2, `P2',  sharpley, `S', observations, `obs') label ctitle(`x' `z' ) nocons noobs 

*** COLUMN (2)

** partial R2
pcorr `x'  know_onlyMF know_onlyMF2 know_excl  $controls
matrix P=r(p_corr)
local P2=P[1,1]^2

xi: reg `x'  know_onlyMF know_onlyMF2 know_excl  $controls  , robust
local obs=e(N)
local r2=e(r2)
est store reg1
shapley2, stat(r2) 
matrix S= e(shapley_rel)
local S=S[1,1]+S[2,1]
sum `x' 
local mean=r(mean)
outreg2 using  `filename'.xls, append keep(know_onlyMF know_onlyMF2 ) bdec(3) sdec(3)  nor2 addstat(mean, `mean', R2, `r2', partial R2, `P2', sharpley, `S', observations, `obs') label ctitle(`x' `z' ) nocons noobs

}

*~*~* Table 5: Targeting

local filename = "Targeting"
capture erase "`filename'.txt"
capture erase "`filename'.xml"

use "SocialIncentives_Data_Tonly" , clear  
set seed 2222

local depvar1 "treated"
loc k=0

foreach z in know ff agri_discussion RelHH  TribeHH  {

foreach y in depvar1 {
foreach x of local `y' {
local k=`k'+1
foreach name in onlyMF_B both_B none_B {
rename `z'_`name' `name'
}

if `k'<6 {
	areg `x'_F  onlyMF_B both_B  none_B  distance_B distance_notMF $controls , a(agri_unique) cluster(cluster`z') 
}
else if `k'==6 {
	areg `x'_F  onlyMF_B both_B  none_B  $controls , a(agri_unique) cluster(cluster`z') 
}
local obs=e(N)
sum `x'_F if  `z'_onlynotMF_B==1
local m =r(mean)
test onlyMF_B =both_B 
local p=r(p)
test onlyMF_B =none_B 
local p2=r(p)
outreg2 using  `filename'.xls, keep(onlyMF_B both_B none_B) nor2 append bdec(3) sdec(3) ctitle(`x' `z')   addstat (mean, `m', p, `p', p2, `p2', observations, `obs') nocons noobs adec(3) afmt(f)
foreach name in onlyMF_B both_B none_B {
rename `name' `z'_`name'
}
}
}
}


*~*~* Table 6: Pro Poor Targeting

local filename = "Targeting"
capture erase "`filename'.txt"
capture erase "`filename'.xml"

use "SocialIncentives_Data_Tonly" , clear  
set seed 2222

local depvar1 "treated"

foreach z in know ff agri_discussion RelHH  TribeHH {
foreach y in depvar1 {
foreach x of local `y' {

gen `z'_notMF_notbottom= `z'_onlynotMF_B*notbottom
gen `z'_MF_notbottom= `z'_onlyMF_B*notbottom
gen `z'_both_notbottom= `z'_both_B*notbottom
gen `z'_none_notbottom= `z'_none_B*notbottom

gen `z'_notMF_bottom=`z'_onlynotMF_B*bottom
gen `z'_MF_bottom= `z'_onlyMF_B*bottom
gen `z'_both_bottom= `z'_both_B*bottom
gen `z'_none_bottom= `z'_none_B*bottom

foreach name in MF_bottom MF_notbottom both_bottom both_notbottom none_bottom none_notbottom {
rename `z'_`name' `name'
}
areg `x'_F bottom MF_bottom MF_notbottom both_bottom both_notbottom none_bottom none_notbottom  distance_B distance_notMF $controls, cluster(cluster`z') a(agri_unique) 
local obs=e(N)
sum `x'_F if `z'_onlynotMF_B==1
local m0`x' =r(mean)
sum `x'_F if `z'_onlynotMF_B==1 & bottom==1
local m1`x'=r(mean)
sum `x'_F if `z'_onlynotMF_B==1 & notbottom==1
local m2`x'=r(mean)

test MF_bottom - MF_notbottom =0
local `x'p1=r(p)

outreg2 using  `filename'.xls,  nor2 append keep(MF_bottom MF_notbottom bottom) bdec(3) sdec(3)  label ctitle(`x' `z' )  nocons  addstat(mean1, `m1`x'' , mean2, `m2`x'' , p1P, ``x'p1', observations, `obs') noobs adec(3) afmt(f) sortvar(MF_bottom MF_notbottom bottom)
foreach name in MF_bottom MF_notbottom both_bottom both_notbottom none_bottom none_notbottom {
rename `name' `z'_`name' 
}
}
}
}




***************************************************************
*~*~* Table 7: Excess Asset Accumulation of Delivery Agents

local filename = "FavorsDA"
capture erase "`filename'.txt"
capture erase "`filename'.xml"

use "SocialIncentives_Data_TandC.dta", clear
set seed 2222

local targetvar "consumption_food2se_B"

foreach t of local targetvar {
bys agri_unique: egen bot`t'=pctile(`t'), p(25)
gen bottom=0
replace bottom=1 if `t'<bot`t'
gen notbottom=0
replace notbottom=1 if `t'>=bot`t'
}

g tieDP=tieD==1&bottom==1
g tieDNP=tieD==1&bottom==0
g tieCP=tieC==1&bottom==1
g tieCNP=tieC==1&bottom==0

bys agri_unique :egen tTP=sum(tieDP)
bys agri_unique :egen tTNP=sum(tieDNP)
bys agri_unique :egen tCP=sum(tieCP)
bys agri_unique :egen tCNP=sum(tieCNP)

gen totP=tCP+tTP
gen totNP=tCNP+tTNP

local k=0

foreach x in total_nb_assets_3 total_value_assets_3  total_value_HH_assets_3  total_value_agri_assets_3  total_value_bsn_assets_3 total_savings2se {
local k=`k'+1
preserve
gen a1=(`x'_W_F -`x'_W_B)/`x'_W_B
gen a2=(`x'_F -`x'_B)/`x'_B 
sum a2
rename `x'_W_B ass1
rename `x'_B ass2

*hypothetical DA predictors
reg a2  ass2 $controls acres_owned_B age_B nb_mktcrops_B netoutput2se_B if agri==0&rank==1
gen alpha=_cons
gen beta=_b[ass2]
gen beta1=_b[branch1]
gen beta2=_b[branch2]
gen beta3=_b[branch3]
gen beta4=_b[acres_owned_B]
gen beta5=_b[age_B]
gen beta6=_b[nb_mktcrops_B]
gen beta7=_b[netoutput2se_B]

gen ahat=alpha+beta*ass1+beta1*branch1+beta2*branch2+beta3*branch3+beta4*acres_owned_B+beta5*age_B+beta6*nb_mktcrops_B +beta7*netoutput2se_B 
foreach var in a1 a2 ahat {
bysort agri_unique: egen mean_`var'=mean(`var')
drop `var'
rename mean_`var' `var'
}
keep if agri==1&u==1

**~*~* TableS

set seed 1234
gen aN=a1 -ahat
replace aN=aN+0.1
gen lnW=ln(aN)
replace lnW=0 if lnW==.

if "`x'" == "total_nb_assets_3" |  "`x'"=="total_value_assets_3" {
reg lnW  tT tot 
local obs=e(N)
local r2=e(r2)
sum lnW 
local mean=r(mean)
outreg2 using `filename'.xls, keep(tT) ctitle(`x') append   nocons  bdec(3) sdec(3) addstat(mean, `mean', R2, `r2', observations, `obs') noobs nor2 adec(3) afmt(f)
}

if "`x'"=="total_value_assets_3" | "`x'"=="total_value_HH_assets_3" | "`x'"=="total_value_agri_assets_3" | "`x'"=="total_value_bsn_assets_3" | "`x'"=="total_savings2se" {
reg lnW  tTP tTNP totP totNP 
local obs=e(N)
local r2=e(r2)
sum lnW 
local mean=r(mean)
outreg2 using `filename'.xls, keep(tTNP tTP ) ctitle(`x') sortvar(tT tTNP tTP) append   nocons  bdec(3) sdec(3) addstat(mean, `mean', R2, `r2', observations, `obs') noobs nor2 adec(3) afmt(f)
}
restore 
}




**********************************
* 2. APPENDIX TABLES AND FIGURES
**********************************


*  Table A1: Balance on Village Characteristics

use "SocialIncentives_Data_TandC" , clear
set seed 2222

collapse (mean) nb_HH perc_farmers short_distance branch_min mean_wealth_index sd_wealth_index agri branch1 branch2 branch3, by(clus_unique)

local Vill "nb_HH perc_farmers short_distance branch_min mean_wealth_index sd_wealth_index "
local depvar `Vill' 

count if agri==0
local count1=r(N)
count if agri==1
local count2=r(N)

foreach x of local depvar{
sum `x'  if agri==0, d
if abs(r(mean))>=100 {
local m1`x': di %12.1fc r(mean)
}
if abs(r(mean))>=10 & abs(r(mean))<100 {
local m1`x': di %12.2fc r(mean)
}
if abs(r(mean))<10 {
local m1`x': di %12.3fc r(mean)
}

if abs(r(sd))>=100 {
local sd1`x': di %12.1fc r(sd)
}
if abs(r(sd))>=10 & abs(r(sd))<100 {
local sd1`x': di %12.2fc r(sd)
}
if abs(r(sd))<10 {
local sd1`x': di %12.3fc r(sd)
}

sum `x'  if agri==1, d
if abs(r(mean))>=100 {
local m2`x': di %12.1fc r(mean)
}
if abs(r(mean))>=10 & abs(r(mean))<100 {
local m2`x': di %12.2fc r(mean)
}
if abs(r(mean))<10 {
local m2`x': di %12.3fc r(mean)
}

if abs(r(sd))>=100 {
local sd2`x': di %12.1fc r(sd)
}
if abs(r(sd))>=10 & abs(r(sd))<100 {
local sd2`x': di %12.2fc r(sd)
}
if abs(r(sd))<10 {
local sd2`x': di %12.3fc r(sd)
}
reg `x' agri  $controls , r 
test agri
local p12`x': di %12.3fc  r(p)
}

display "count, `count1' , `count2'"
foreach x of local depvar{
display "`x'" "," `m1`x'' "," `m2`x'' ","    `p12`x'' 
display "`x'" "," `sd1`x'' ","  `sd2`x'' ","    
}

*~*~* Table A2: Attrition

local filename = "Attrition"
capture erase "`filename'.txt"
capture erase "`filename'.xml"

**** COLUMNS 1-3

use "SocialIncentives_Data_TandC" , clear
set seed 2222

local HH "wealth_index_B  primary_B acres_owned_B consumption_food2se_B  total_value_agri_assets2se_B"
local Agri "know_seeds_B belief_seeds_B adopt_seeds_ever_B nb_techniques_known_B  adopt_tech_ever_B  hours_agri_day_B acres_cultivated_B  nb_cropsN_B nb_mktcrops_B  share_output_sold_B  netoutput2se_B"
local depvar `HH' `Agri'

rename missing_endline missing_endline_B


foreach x in missing_endline{
reg  `x' agri $controls , r  cluster(clus_unique)
local obs=e(N)
sum `x' 
local m`x'=r(mean)
outreg2 using `filename'.xls, replace append keep(agri) sdec(3) bdec(3)  label ctitle(`y') addstat(mean, `m`x'', observations, `obs') nocons noobs nor2 adec(3) afmt(f)
}

foreach x in missing_endline{
reg  `x' agri `depvar' $controls , r  cluster(clus_unique)
local obs=e(N)
sum `x'
local m`x'=r(mean)
outreg2 using `filename'.xls, append keep(agri) sdec(3) bdec(3) label ctitle(`y') addstat(mean, `m`x'', observations, `obs') nocons noobs nor2 adec(3) afmt(f)
}

foreach x in missing_endline{
foreach y of local depvar{
di " `y'"
gen vag_`y'=agri*`y'
}

reg  `x' agri `depvar' vag* $controls , r  cluster(clus_unique)
local obs=e(N)
sum `x'
local m`x'=r(mean)
foreach var of varlist vag* {
local inter `var' `inter'
}
test `inter'
local p=r(p)
outreg2 using `filename'.xls, append keep(agri)  bdec(3) sdec(3) label ctitle(`y') addstat(mean, `m`x'', p-value,`p', observations, `obs') nocons noobs nor2 adec(3) afmt(f)
}

**** Columns 4-6


local z know

foreach var in `z'_onlyMF_B `z'_onlynotMF_B `z'_both_B  `z'_none_B {
replace `var'=0  if agri==0
}

foreach x in missing_endline{
reg `x' `z'_onlyMF_B `z'_onlynotMF_B `z'_both_B  `z'_none_B  $controls ,r  cluster(clus_unique)
local obs=e(N)
sum `x' 
local m`x'=r(mean)
outreg2 using `filename'.xls,  keep(`z'_onlyMF_B `z'_onlynotMF_B ) sdec(3) bdec(3)  label ctitle(`y') addstat(mean, `m`x'', observations, `obs') nocons noobs nor2 adec(3) afmt(f)
}

foreach x in missing_endline{
reg `x' `z'_onlyMF_B `z'_onlynotMF_B `z'_both_B  `z'_none_B `depvar' $controls , r  cluster(clus_unique)
local obs=e(N)
sum `x' 
local m`x'=r(mean)
outreg2 using `filename'.xls,  keep(`z'_onlyMF_B `z'_onlynotMF_B ) sdec(3) bdec(3) label ctitle(`y') addstat(mean, `m`x'', observations, `obs') nocons  nor2 noobs adec(3) afmt(f)
}

foreach x in missing_endline{
local s=0
foreach y of local depvar{
local s=`s'+1
di "`y'"
gen v_kMF_`s'=`z'_onlyMF_B*`y'
gen v_knMF_`s'=`z'_onlynotMF_B*`y'
gen both_`s'=`z'_both_B*`y'
gen none_`s'=`z'_none_B*`y'
}

reg `x' `z'_onlyMF_B `z'_onlynotMF_B `z'_both_B  `z'_none_B  `depvar' v_kMF_* v_knMF_* both* none* $controls, r  cluster(clus_unique)
local obs=e(N)
sum `x' 
local m`x'=r(mean)

forval i=1/`s' {
test  v_kMF_`i'= v_knMF_`i', accum
}
local p=r(p)
outreg2 using `filename'.xls,  keep(`z'_onlyMF_B `z'_onlynotMF_B )  bdec(3) sdec(3) label ctitle(`y') addstat(mean, `m`x'', p-value,`p', observations, `obs') sortvar(agri `z'_onlyMF_B `z'_onlynotMF_B) nocons nor2 noobs adec(3) afmt(f)
}

*~*~* Table A3: Aggregate Impacts on Intrahousehold Outcomes

local filename = "table"
capture erase "`filename'.txt"
capture erase "`filename'.xml"

use "SocialIncentives_Data_TandC", clear
set seed 2222

local rep=500

g treated_B=0

gen share_F= q7104b_n_F/q7101_F
replace share_F=0 if q7101_F==0
gen share_B= q7104b_n_B/q7101_B
replace share_B=0 if q7101_B==0

local depvar1 "q7102 share"

local k=0
foreach x of local depvar1{
local k=`k'+1
gen var`k'=`x'_F
cap sum `x'_B
cap gen `x'_B=0
if r(N)==0 {
replace `x'_B=0 if `x'_B==.
}
gen var`k'_B=`x'_B
}

preserve
keep var* $controls agri clus_unique
rwolf var1 var2, ///
indepvar(agri)  controls($controls var1_B var2_B) ///
reps(`rep') vce(cluster clus_unique) seed(2222) cluster(clus_unique)
restore
local k=0
foreach x of varlist var1 var2 {
local k=`k'+1
/*pv*/ local pv_mht`k': di %12.3fc e(rw_`x')
}

drop var*
local k=0
foreach x of local depvar1{
local k=`k'+1
preserve
keep `x'_F agri  $controls clus_unique `x'_B
randcmd ((agri) reg `x'_F agri  $controls `x'_B,  cluster(clus_unique)), treatvars(agri) reps(`rep') seed(2222)
restore
matrix r=e(RCoef)
local p_ri:  di %12.3fc  r[1,3]

xi: reg  `x'_F agri  $controls `x'_B, r cl(clus_unique)
local obs=e(N)
sum `x'_F if agri==0
local m`x'=r(mean)
local k=`k'
outreg2 agri using `filename'.xls,  keep(agri) bdec(3) sdec(3) label ctitle(`x') addstat(p-value RI, `p_ri', p-value MHT, `pv_mht`k'', mean control, `m`x'', observations, `obs') nocons nor2 noobs adec(3) afmt(f)
}

*~*~* Table A4: Interaction with the Microfinance Program

local filename = "Microfinance"
capture erase "`filename'.txt"
capture erase "`filename'.xml"

use "SocialIncentives_Data_TandC", clear

local rep=500

set seed 2222


local depvar1 " treated adopt_seedsY_MF trained_MF  adopt_seedsY_BRACnotMF adopt_seedsY_notBRAC mean_obs_tech "

** MHT
local k=0
foreach x of local depvar1{
local k=`k'+1
gen var`k'=`x'_F
}

preserve
keep var* $controls group1 group3 clus_unique
rwolf var1 var2 var3 var4 var5, ///
indepvar(group1 group3) seed(2222) controls($controls) ///
reps(`rep') vce(cluster clus_unique) cluster(clus_unique)
restore

local k=0
foreach x of varlist  var1 var2 var3 var4 var5 {
local k=`k'+1
/*pv*/ local pv_mht1`k': di %12.3fc e(rw_`x'_group1)
/*pv*/ local pv_mht2`k': di %12.3fc e(rw_`x'_group3)
}

preserve
keep var* $controls group1 group3 clus_unique
rwolf var6 , ///
indepvar(group1 group3) seed(2222) controls($controls) ///
reps(`rep') vce(cluster clus_unique) cluster(clus_unique)
restore

foreach x of varlist var6  {
local k=`k'+1
/*pv*/ local pv_mht1`k': di %12.3fc e(rw_`x'_group1)
/*pv*/ local pv_mht2`k': di %12.3fc e(rw_`x'_group3)
}

drop var*

local k=0
foreach x of local depvar1{
local k=`k'+1
cap sum `x'_B
cap gen `x'_B=0

if r(N)==0 {
replace `x'_B=0 if `x'_B==.
}
preserve
keep `x'_F group1 group3  $controls clus_unique
randcmd ((group1 group3) reg `x'_F group1 group3  $controls,  cluster(clus_unique)), seed(2222) treatvars(group1 group3) reps(`rep') 
restore
matrix r=e(RCoef)
local p_ri1:  di %12.3fc  r[1,3]
local p_ri2:  di %12.3fc  r[2,3]


xi: reg  `x'_F group1 group3  $controls, r cl(clus_unique)
local obs=e(N)
sum `x'_F if group1==0 & group3==0 
local m`x'=r(mean)

test group1=group3
local pv=r(p)
outreg2  using `filename'.xls, append  keep(group1 group3) bdec(3) sdec(3) label ctitle(`x') addstat( p-value RI 1, `p_ri1', p-value MHT 1, `pv_mht1`k'', p-value RI 2, `p_ri2', p-value MHT 2, `pv_mht2`k'',  mean control, `m`x'',  p-value, `pv', observations, `obs') nocons nor2 noobs adec(3) afmt(f)
}


local rep=500

replace consumption_food2se_F=consumption_food2se_F*(12/52)

** MHT
local depvar1 " netoutput2se  nb_mktcrops  consumption_food2se total_cons total_value_agri_assets2se"

local k=0
foreach x of local depvar1{
local k=`k'+1
gen var`k'=`x'_F
cap sum `x'_B
cap gen `x'_B=0
if r(N)==0 {
replace `x'_B=0 if `x'_B==.
}
gen var`k'_B=`x'_B
}

local depvar1 " netoutput2se nb_mktcrops  consumption_food2se total_cons total_value_agri_assets2se"

preserve
keep var* $controls group1 group3 clus_unique
rwolf var1 var2   , ///
indepvar(group1 group3)  controls($controls var1_B var2_B) ///
reps(`rep') vce(cluster clus_unique) seed(2222) cluster(clus_unique)
restore
local k=0
foreach x of varlist var1 var2   {
local k=`k'+1
/*pv*/ local pv_mht1`k': di %12.3fc e(rw_`x'_group1)
/*pv*/ local pv_mht2`k': di %12.3fc e(rw_`x'_group3)
}

preserve
keep var* $controls group1 group3 clus_unique
rwolf var3 var4 var5 , ///
indepvar(group1 group3) seed(2222) controls($controls var3_B var3_B) ///
reps(`rep') vce(cluster clus_unique) cluster(clus_unique)
restore
foreach x of varlist  var3 var4 var5 {
local k=`k'+1
/*pv*/ local pv_mht1`k': di %12.3fc e(rw_`x'_group1)
/*pv*/ local pv_mht2`k': di %12.3fc e(rw_`x'_group3)
}

drop var*

local k=0
foreach x of local depvar1{
local k=`k'+1
cap sum `x'_B
cap gen `x'_B=0

if r(N)==0 {
replace `x'_B=0 if `x'_B==.
}
preserve
keep `x'_F group1 group3  $controls clus_unique `x'_B
randcmd ((group1 group3) reg `x'_F `x'_B group1 group3  $controls,  cluster(clus_unique)), seed(2222) treatvars(group1 group3) reps(`rep') 
restore
matrix r=e(RCoef)
local p_ri1:  di %12.3fc  r[1,3]
local p_ri2:  di %12.3fc  r[2,3]


xi: reg  `x'_F `x'_B group1 group3  $controls, r cl(clus_unique)
local obs=e(N)
sum `x'_F if group1==0 & group3==0 
local m`x'=r(mean)

test group1=group3
local pv=r(p)
outreg2  using `filename'.xls, keep(group1 group3) bdec(3) sdec(3) label ctitle(`x') addstat( p-value RI 1, `p_ri1', p-value MHT 1, `pv_mht1`k'', p-value RI 2, `p_ri2', p-value MHT 2, `pv_mht2`k'',  mean control, `m`x'',  p-value, `pv', observations, `obs') nocons nor2 noobs adec(3) afmt(f)
}

*~*~* Table A5: Targeting and Techniques

local filename = "Targeting"
capture erase "`filename'.txt"
capture erase "`filename'.xml"

use "SocialIncentives_Data_Tonly" , clear  
set seed 2222

local depvar1 "mean_obs_tech "
loc k=0

foreach z in know ff agri_discussion  {

foreach y in depvar1 {
foreach x of local `y' {
local k=`k'+1
foreach name in onlyMF_B both_B none_B {
rename `z'_`name' `name'
} 
if `k'<6 {
	areg `x'_F  onlyMF_B both_B  none_B  distance_B distance_notMF $controls , a(agri_unique) cluster(cluster`z') 
}
else if `k'==6 {
	areg `x'_F  onlyMF_B both_B  none_B  $controls , a(agri_unique) cluster(cluster`z') 
}
local obs=e(N)
sum `x'_F if  `z'_onlynotMF_B==1
local m =r(mean)
test onlyMF_B =both_B 
local p=r(p)
test onlyMF_B =none_B 
local p2=r(p)
outreg2 using  `filename'.xls, keep(onlyMF_B both_B  none_B) nor2 append bdec(3) sdec(3)    addstat (mean, `m', p, `p', p2, `p2', observations, `obs') nocons noobs adec(3) afmt(f) ctitle(`x' `z')
foreach name in onlyMF_B both_B none_B {
rename `name' `z'_`name'
}
}
}
}

*~*~* Table A6: p-value Corrections for Randomization Inference

local filename = "RI-MHT_p-values"
capture erase "`filename'.txt"
capture erase "`filename'.xml"


use "SocialIncentives_Data_Tonly" , clear  
set seed 2222

local depvar1 "treated "

rename TribeHH* tHH*
rename agri_discussion* agdi*
rename clusteragri_discussion clusteragdi

foreach y in depvar1 {

foreach x of local `y' {

local k=0

foreach z in know ff agdi RelHH  tHH {

local k=`k'+1

preserve
keep var*  `x'_F  `z'_onlyMF_B  `z'_both_B  `z'_none_B distance_B distance_notMF $controls agri_unique cluster
randcmd (( `z'_onlyMF_B) areg `x'_F  `z'_onlyMF_B `z'_both_B  `z'_none_B  distance_B distance_notMF $controls, cluster(cluster) a(agri_unique)), seed(2222) treatvars(`z'_onlyMF_B) reps(500)
restore
matrix r=e(RCoef)
local p_ri1:  di %12.3fc  r[1,1]

areg `x'_F  `z'_onlyMF_B `z'_both_B  `z'_none_B  distance_B distance_notMF $controls, cluster(cluster) a(agri_unique)
outreg2 using  `filename'.xls,  nor2 drop(*) ctitle(`x' `z' )  nocons  addstat(RI1, `p_ri1')    adec(3)  noomitted
}
}
}


foreach z in know ff agdi RelHH  tHH {

gen `z'_onlynotMF_notbottom= `z'_onlynotMF_B*notbottom
gen `z'_onlyMF_notbottom= `z'_onlyMF_B*notbottom
gen `z'_both_notbottom= `z'_both_B*notbottom
gen `z'_none_notbottom= `z'_none_B*notbottom

gen `z'_onlynotMF_bottom=`z'_onlynotMF_B*bottom
gen `z'_onlyMF_bottom= `z'_onlyMF_B*bottom
gen `z'_both_bottom= `z'_both_B*bottom
gen `z'_none_bottom= `z'_none_B*bottom

}


local depvar1 "treated"
foreach y in depvar1 {

foreach x of local `y' {

local k=0
foreach z in know ff agdi RelHH  tHH {

local k=`k'+1

preserve
keep var*  `x'_F  bottom `z'_onlyMF_bottom `z'_onlyMF_notbottom  `z'_both_bottom `z'_both_notbottom  `z'_none_bottom `z'_none_notbottom  distance_B distance_notMF $controls agri_unique cluster
randcmd ((`z'_onlyMF_bottom `z'_onlyMF_notbottom bottom ) areg `x'_F bottom `z'_onlyMF_bottom `z'_onlyMF_notbottom `z'_both_bottom `z'_both_notbottom  `z'_none_bottom `z'_none_notbottom  distance_B distance_notMF $controls, cluster(cluster) a(agri_unique)), seed(2222) treatvars(`z'_onlyMF_bottom `z'_onlyMF_notbottom) reps(500)
restore
matrix r=e(RCoef)
local p_ri1:  di %12.3fc  r[1,3]
local p_ri2:  di %12.3fc  r[2,3]
local p_ri3:  di %12.3fc  r[3,3]

areg `x'_F bottom `z'_onlyMF_bottom `z'_onlyMF_notbottom `z'_both_bottom `z'_both_notbottom  `z'_none_bottom `z'_none_notbottom  distance_B distance_notMF $controls, cluster(cluster) a(agri_unique)
outreg2 using  `filename'.xls,  nor2 drop(*) ctitle(`x' `z' )  nocons  addstat(RI1, `p_ri1',RI2, `p_ri2', RI3, `p_ri3')    adec(3) noomitted
}
}
}

*~*~* Table A7: Pro Poor Targeting, Robustness to Alternative Consumption Thresholds for Poverty

local filename = "Targeting"
capture erase "`filename'.txt"
capture erase "`filename'.xml"

************** bottom quartile

use "SocialIncentives_Data_Tonly" , clear  
set seed 2222

local depvar1 "treated"

drop bottom notbottom

local targetvar "consumption_food2se_B"

foreach t of local targetvar {
bys agri_unique: egen bot`t'=pctile(`t'), p(10)
gen bottom=0
replace bottom=1 if `t'<bot`t'
gen notbottom=0
replace notbottom=1 if `t'>=bot`t'
}

rename agri_discussion* agdi*
rename clusteragri_discussion clusteragdi

rename know_close_MF_* ff_MF_*
rename know_close_notMF_* ff_notMF_*

foreach z in know ff agdi {
*ff agdi RelHH  TribeHH {
foreach y in depvar1 {
foreach x of local `y' {

gen `z'_notMF_notbottom= `z'_notMF_B*notbottom
gen `z'_MF_notbottom= `z'_MF_B*notbottom
gen `z'_both_notbottom= `z'_both_B*notbottom
gen `z'_none_notbottom= `z'_none_B*notbottom

gen `z'_notMF_bottom=`z'_notMF_B*bottom
gen `z'_MF_bottom= `z'_MF_B*bottom
gen `z'_both_bottom= `z'_both_B*bottom
gen `z'_none_bottom= `z'_none_B*bottom

foreach name in MF_bottom MF_notbottom none_bottom none_notbottom {
rename `z'_`name' `name'
}

areg `x'_F bottom MF_bottom MF_notbottom  none_bottom none_notbottom  distance_B distance_notMF $controls, cluster(cluster) a(agri_unique)
local obs=e(N)

sum `x'_F if `z'_notMF_B==1
local m0`x' =r(mean)
sum `x'_F if `z'_notMF_B==1 & bottom==1
local m1`x'=r(mean)
sum `x'_F if `z'_notMF_B==1 & notbottom==1
local m2`x'=r(mean)

*** Poor*(DA ties & H tie - CA ties & H ties) -  Not Poor*(DA ties & H tie - CA ties & H ties)
test MF_bottom - MF_notbottom =0
local `x'p1=r(p)

test bottom + MF_bottom - MF_notbottom =0
local `x'p2=r(p)

test bottom - MF_notbottom =0
local `x'p3=r(p)

outreg2 using  `filename'.xls,  nor2 append keep(bottom MF_bottom MF_notbottom  ) bdec(3) sdec(3)  label ctitle(`x' `z' )  nocons  addstat( mean1, `m1`x'' , mean2, `m2`x'' , p1P, ``x'p1', observations, `obs') noobs adec(3) afmt(f)

foreach name in MF_bottom MF_notbottom none_bottom none_notbottom {
rename `name' `z'_`name'
}

}
}
}

************** median 

use "SocialIncentives_Data_Tonly" , clear  
set seed 2222

local depvar1 "treated"

drop bottom notbottom

local targetvar "consumption_food2se_B"

foreach t of local targetvar {
bys agri_unique: egen bot`t'=pctile(`t'), p(50)
gen bottom=0
replace bottom=1 if `t'<bot`t'
gen notbottom=0
replace notbottom=1 if `t'>=bot`t'
}

rename agri_discussion* agdi*
rename clusteragri_discussion clusteragdi

rename know_close_MF_* ff_MF_*
rename know_close_notMF_* ff_notMF_*

foreach z in know ff agdi {
*ff agdi RelHH  TribeHH {
foreach y in depvar1 {
foreach x of local `y' {

gen `z'_notMF_notbottom= `z'_notMF_B*notbottom
gen `z'_MF_notbottom= `z'_MF_B*notbottom
gen `z'_both_notbottom= `z'_both_B*notbottom
gen `z'_none_notbottom= `z'_none_B*notbottom

gen `z'_notMF_bottom=`z'_notMF_B*bottom
gen `z'_MF_bottom= `z'_MF_B*bottom
gen `z'_both_bottom= `z'_both_B*bottom
gen `z'_none_bottom= `z'_none_B*bottom

foreach name in MF_bottom MF_notbottom none_bottom none_notbottom {
rename `z'_`name' `name'
}

areg `x'_F bottom MF_bottom MF_notbottom  none_bottom none_notbottom  distance_B distance_notMF $controls, cluster(cluster) a(agri_unique)
local obs=e(N)

sum `x'_F if `z'_notMF_B==1
local m0`x' =r(mean)
sum `x'_F if `z'_notMF_B==1 & bottom==1
local m1`x'=r(mean)
sum `x'_F if `z'_notMF_B==1 & notbottom==1
local m2`x'=r(mean)

*** Poor*(DA ties & H tie - CA ties & H ties) -  Not Poor*(DA ties & H tie - CA ties & H ties)
test MF_bottom - MF_notbottom =0
local `x'p1=r(p)

test bottom + MF_bottom - MF_notbottom =0
local `x'p2=r(p)

test bottom - MF_notbottom =0
local `x'p3=r(p)

outreg2 using  `filename'.xls,  nor2 append keep(bottom MF_bottom MF_notbottom ) bdec(3) sdec(3)  label ctitle(`x' `z' )  nocons sortvar(MF_bottom MF_notbottom bottom) addstat(mean1, `m1`x'' , mean2, `m2`x'' , p1P, ``x'p1', observations, `obs') noobs adec(3) afmt(f)

foreach name in MF_bottom MF_notbottom none_bottom none_notbottom {
rename `name' `z'_`name'
}

}
}
}

*~*~* Table A8: Pro Poor Targeting, Robustness to Non Consumption Based Measures of Poverty

local filename = "Targeting"
capture erase "`filename'.txt"
capture erase "`filename'.xml"

local target " wealth_index_B total_value_assets_3_B adopt_tech_ever_B netoutput2se_B"

foreach t of local target{
use "SocialIncentives_Data_Tonly" , clear  
set seed 2222

local depvar1 "treated"

drop bottom notbottom


bys agri_unique: egen bot`t'=pctile(`t'), p(25)
gen bottom=0
replace bottom=1 if `t'<bot`t'
gen notbottom=0
replace notbottom=1 if `t'>=bot`t'


rename agri_discussion* agdi*
rename clusteragri_discussion clusteragdi

rename know_close_MF_* ff_MF_*
rename know_close_notMF_* ff_notMF_*

foreach z in know ff agdi {
*ff agdi RelHH  TribeHH {
foreach y in depvar1 {
foreach x of local `y' {

gen `z'_notMF_notbottom= `z'_notMF_B*notbottom
gen `z'_MF_notbottom= `z'_MF_B*notbottom
gen `z'_both_notbottom= `z'_both_B*notbottom
gen `z'_none_notbottom= `z'_none_B*notbottom

gen `z'_notMF_bottom=`z'_notMF_B*bottom
gen `z'_MF_bottom= `z'_MF_B*bottom
gen `z'_both_bottom= `z'_both_B*bottom
gen `z'_none_bottom= `z'_none_B*bottom

foreach name in MF_bottom MF_notbottom none_bottom none_notbottom {
rename `z'_`name' `name'
}

areg `x'_F bottom MF_bottom MF_notbottom  none_bottom none_notbottom  distance_B distance_notMF $controls, cluster(cluster) a(agri_unique)
local obs=e(N)
sum `x'_F if `z'_notMF_B==1
local m0`x' =r(mean)
sum `x'_F if `z'_notMF_B==1 & bottom==1
local m1`x'=r(mean)
sum `x'_F if `z'_notMF_B==1 & notbottom==1
local m2`x'=r(mean)

*** Poor*(DA ties & H tie - CA ties & H ties) -  Not Poor*(DA ties & H tie - CA ties & H ties)
test MF_bottom - MF_notbottom =0
local `x'p1=r(p)

test bottom + MF_bottom - MF_notbottom =0
local `x'p2=r(p)

test bottom - MF_notbottom =0
local `x'p3=r(p)

outreg2 using  `filename'.xls,  nor2 append keep(bottom MF_bottom MF_notbottom  ) bdec(3) sdec(3)  label ctitle(`x' `z' )  nocons sortvar(MF_bottom MF_notbottom bottom) addstat(mean1, `m1`x'' , mean2, `m2`x'' , p1P, ``x'p1', observations, `obs')  noobs adec(3) afmt(f)

foreach name in MF_bottom MF_notbottom none_bottom none_notbottom {
rename `name' `z'_`name'
}

}
}
}
}

*~*~* Table A9: Profit Impacts of Being Targeted by DAs or Receiving Seeds from Any Source

local filename = "profitimpact"
capture erase "`filename'.txt"
capture erase "`filename'.xml"


use "SocialIncentives_Data_TandC.dta", clear
set seed 2222

local targetvar "consumption_food2se_B"

foreach t of local targetvar {
bys clus_unique: egen bot`t'=pctile(`t'), p(25)
gen bottom=0
replace bottom=1 if `t'<bot`t'
gen notbottom=0
replace notbottom=1 if `t'>=bot`t'
}
g treated2_F=adopt_seedsY_MF_F
replace treated2_F=1 if adopt_seedsY_BRACnotMF_F==1| adopt_seedsY_notBRAC_F==1

local depvar1 "netoutput2se "
g agri_bottom=agri*bottom
g agri_notbottom=agri*(1-bottom)
g treated_bottom=treated_F*bottom
g treated_notbottom=treated_F*(1-bottom)
g treated2_bottom=treated2_F*bottom
g treated2_notbottom=treated2_F*(1-bottom)
g nottreated_F=1 if treated_F==0
replace nottreated_F=0 if treated_F==1
g nottreat_notbottom = nottreated_F*(1-bottom)
g nottreated2_F=1 if treated2_F==0
replace nottreated2_F=0 if treated2_F==1
g nottreat2_notbottom = nottreated2_F*(1-bottom)

foreach y in depvar1 {
foreach x of local `y' {


reg `x'_F bottom  treated_bottom treated_notbottom $controls , cluster(clus_unique) 
local obs=e(N)
test treated_bottom - treated_notbottom=0
local p=r(p)
sum `x'_F if agri==0, d
local mean_control=r(mean)
outreg2 using  `filename'.xls,  nor2 append keep(`x'_F bottom treated_bottom treated_notbottom) bdec(3) sdec(3)  label ctitle(`x' `z' )  nocons  addstat(mean_control, `mean_control',p,`p', observations, `obs' ) noobs 

reg `x'_F bottom  treated2_bottom treated2_notbottom $controls , cluster(clus_unique) 
local obs=e(N)
test treated2_bottom - treated2_notbottom=0
local p=r(p)
sum `x'_F if agri==0, d
local mean_control=r(mean)
outreg2 using  `filename'.xls,  nor2 append keep(`x'_F bottom  treated2_bottom treated2_notbottom) bdec(3) sdec(3)  label ctitle(`x' `z' )  nocons  addstat(mean_control, `mean_control', p,`p', observations, `obs'  )  noobs sortvar(treated_bottom treated_notbottom treated2_bottom treated2_notbottom bottom)

}
}

*~*~* Table A10: Diffusion and Informal Transfers

local filename = "Diffusion_Transfers"
capture erase "`filename'.txt"
capture erase "`filename'.xml"

use "SocialIncentives_Data_Tonly" , clear  
set seed 2222

*** Columns (1) - (2)


local depvar1 "adopt_seedsY_notBRAC adopt_seedsY"

foreach z in know{
foreach y in depvar1 {
foreach x of local `y' {


gen `z'_onlynotMF_notbottom= `z'_onlynotMF_B*notbottom
gen `z'_onlyMF_notbottom= `z'_onlyMF_B*notbottom
gen `z'_both_notbottom= `z'_both_B*notbottom
gen `z'_none_notbottom= `z'_none_B*notbottom

gen `z'_onlynotMF_bottom=`z'_onlynotMF_B*bottom
gen `z'_onlyMF_bottom= `z'_onlyMF_B*bottom
gen `z'_both_bottom= `z'_both_B*bottom
gen `z'_none_bottom= `z'_none_B*bottom

areg `x'_F bottom `z'_onlyMF_bottom `z'_onlyMF_notbottom `z'_both_bottom `z'_both_notbottom  `z'_none_bottom `z'_none_notbottom  distance_B distance_notMF $controls, cluster(cluster) a(agri_unique)
local obs=e(N)
sum `x'_F if `z'_onlynotMF_B==1
local m0`x' =r(mean)
sum `x'_F if `z'_onlynotMF_B==1 & bottom==1
local m1`x'=r(mean)
sum `x'_F if `z'_onlynotMF_B==1 & notbottom==1
local m2`x'=r(mean)

*** Poor*(DA ties & H tie - CA ties & H ties) -  Not Poor*(DA ties & H tie - CA ties & H ties)
test `z'_onlyMF_bottom - `z'_onlyMF_notbottom =0
local `x'p1=r(p)

outreg2 using  `filename'.xls,  nor2 append keep( bottom  `z'_onlyMF_bottom `z'_onlyMF_notbottom  ) bdec(3) sdec(3)  label ctitle(`x' `z' )  nocons  addstat( mean, `m0`x'',  mean1, `m1`x'' , mean2, `m2`x'' , p1P, ``x'p1', observations, `obs')  noobs adec(3) afmt(f)
drop *_notbottom *_bottom

}
}
}


*** Columns (3) - (6)

local depvar1 "  netTrans_ext value_netTransf2se "

foreach z in know{
foreach y in depvar1 {
foreach x of local `y' {

gen `z'_onlynotMF_notbottom= `z'_onlynotMF_B*notbottom
gen `z'_onlyMF_notbottom= `z'_onlyMF_B*notbottom
gen `z'_both_notbottom= `z'_both_B*notbottom
gen `z'_none_notbottom= `z'_none_B*notbottom

gen `z'_onlynotMF_bottom=`z'_onlynotMF_B*bottom
gen `z'_onlyMF_bottom= `z'_onlyMF_B*bottom
gen `z'_both_bottom= `z'_both_B*bottom
gen `z'_none_bottom= `z'_none_B*bottom

areg `x'_F bottom `z'_onlyMF_bottom `z'_onlyMF_notbottom `z'_both_bottom `z'_both_notbottom  `z'_none_bottom `z'_none_notbottom  distance_B distance_notMF $controls, cluster(cluster) a(agri_unique)
local obs=e(N)
sum `x'_F if `z'_onlynotMF_B==1
local m0`x' =r(mean)
sum `x'_F if `z'_onlynotMF_B==1 & bottom==1
local m1`x'=r(mean)
sum `x'_F if `z'_onlynotMF_B==1 & notbottom==1
local m2`x'=r(mean)

*** Poor*(DA ties & H tie - CA ties & H ties) -  Not Poor*(DA ties & H tie - CA ties & H ties)
test `z'_onlyMF_bottom - `z'_onlyMF_notbottom =0
local `x'p1=r(p)

outreg2 using  `filename'.xls,  nor2 append keep(bottom  `z'_onlyMF_bottom `z'_onlyMF_notbottom  ) bdec(3) sdec(3)  label ctitle(`x' `z' )  nocons  addstat( mean, `m0`x'',  mean1, `m1`x'' , mean2, `m2`x'' , p1P, ``x'p1', observations, `obs')   noobs sortvar(`z'_onlyMF_bottom `z'_onlyMF_notbottom   bottom) adec(3) afmt(f)
drop *_notbottom *_bottom

}
}
}


*~*~* Table A11: Agricultural Outcomes of Delivery Agents

local filename = "FavorsDA"
capture erase "`filename'.txt"
capture erase "`filename'.xml"

use "SocialIncentives_Data_TandC.dta", clear
set seed 2222

local targetvar "consumption_food2se_B"

foreach t of local targetvar {
bys agri_unique: egen bot`t'=pctile(`t'), p(25)
gen bottom=0
replace bottom=1 if `t'<bot`t'
gen notbottom=0
replace notbottom=1 if `t'>=bot`t'
}

g tieDP=tieD==1&bottom==1
g tieDNP=tieD==1&bottom==0
g tieCP=tieC==1&bottom==1
g tieCNP=tieC==1&bottom==0

bys agri_unique :egen tTP=sum(tieDP)
bys agri_unique :egen tTNP=sum(tieDNP)
bys agri_unique :egen tCP=sum(tieCP)
bys agri_unique :egen tCNP=sum(tieCNP)

gen totP=tCP+tTP
gen totNP=tCNP+tTNP

local k=0

foreach x in   acres_cultivated hours_agri_day revenuesFIX2se expenditures2se   {
local k=`k'+1
preserve
gen a1=(`x'_W_F -`x'_W_B)/`x'_W_B
gen a2=(`x'_F -`x'_B)/`x'_B 
sum a2
rename `x'_W_B ass1
rename `x'_B ass2

*hypothetical DA predictors
reg a2  ass2 $controls acres_owned_B age_B nb_mktcrops_B netoutput2se_B if agri==0&rank==1
gen alpha=_cons
gen beta=_b[ass2]
gen beta1=_b[branch1]
gen beta2=_b[branch2]
gen beta3=_b[branch3]
gen beta4=_b[acres_owned_B]
gen beta5=_b[age_B]
gen beta6=_b[nb_mktcrops_B]
gen beta7=_b[netoutput2se_B]

gen ahat=alpha+beta*ass1+beta1*branch1+beta2*branch2+beta3*branch3+beta4*acres_owned_B+beta5*age_B+beta6*nb_mktcrops_B +beta7*netoutput2se_B 
foreach var in a1 a2 ahat {
bysort agri_unique: egen mean_`var'=mean(`var')
drop `var'
rename mean_`var' `var'
}
keep if agri==1&u==1

set seed 1234
gen aN=a1 -ahat
replace aN=aN+0.1
gen lnW=ln(aN)
replace lnW=0 if lnW==.

reg lnW  tT tot 
local obs=e(N)
local r2=e(r2)
sum lnW 
local mean=r(mean)
outreg2 using `filename'.xls, keep(tT) ctitle(`x') append   nocons  bdec(3) sdec(3) addstat(mean, `mean', R2, `r2', observations, `obs') noobs nor2 adec(3) afmt(f)

reg lnW  tTP tTNP totP totNP 
local obs=e(N)
local r2=e(r2)
sum lnW 
local mean=r(mean)
outreg2 using `filename'.xls, keep(tTNP tTP ) ctitle(`x') sortvar(tT tTNP tTP) append   nocons  bdec(3) sdec(3) addstat(mean, `mean', R2, `r2', observations, `obs') noobs nor2 adec(3) afmt(f)

restore 
}

*~*~* Table A12: Financial and Social Status of Delivery Agents

local filename = "FavorsDA"
capture erase "`filename'.txt"
capture erase "`filename'.xml"

use "SocialIncentives_Data_TandC.dta", clear
set seed 2222

local targetvar "consumption_food2se_B"

foreach t of local targetvar {
bys agri_unique: egen bot`t'=pctile(`t'), p(25)
gen bottom=0
replace bottom=1 if `t'<bot`t'
gen notbottom=0
replace notbottom=1 if `t'>=bot`t'
}

g tieDP=tieD==1&bottom==1
g tieDNP=tieD==1&bottom==0
g tieCP=tieC==1&bottom==1
g tieCNP=tieC==1&bottom==0

bys agri_unique :egen tTP=sum(tieDP)
bys agri_unique :egen tTNP=sum(tieDNP)
bys agri_unique :egen tCP=sum(tieCP)
bys agri_unique :egen tCNP=sum(tieCNP)

gen totP=tCP+tTP
gen totNP=tCNP+tTNP

replace soc_status_W_F=5-soc_status_W_F

foreach x in fin_status soc_status fin_problems  {
*time_MF_CLP
preserve

keep if agri==1&u==1

**~*~* TableS

reg `x'_W_F  tT tot ,r
local obs=e(N)
local r2=e(r2)
sum `x'_W_F
local mean=r(mean)
outreg2 using `filename'.xls, keep(tT ) ctitle(`x') sortvar(tT tot ) append   nocons  bdec(3) sdec(3) addstat(mean, `mean', R2, `r2', observations, `obs') noobs nor2 adec(3) afmt(f)

reg `x'_W_F  tTP tTNP tot ,r
local obs=e(N)
local r2=e(r2)
sum `x'_W_F
local mean=r(mean)
outreg2 using `filename'.xls, keep(tTNP tTP ) ctitle(`x') sortvar(tT tTNP tTP) append   nocons  bdec(3) sdec(3) addstat(mean, `mean', R2, `r2', observations, `obs') noobs nor2 adec(3) afmt(f)

restore 
}

use "SocialIncentives_Data_TandC.dta", clear
sum adopt_tech_ever_B, d
gen med_tech=1 if adopt_tech_ever_B>3 & adopt_tech_ever_B!=.
replace med_tech=0 if adopt_tech_ever_B<=3 & adopt_tech_ever_B!=.
local k=0
foreach y in netoutput2se_B {
local k=`k'+1
reg `y' $controls 
predict res_y`k' if `y'!=., res
}



* Figure A1: Variation in Number of Social Ties

use "SocialIncentives_Data_Tonly" , clear 
set seed 2222
cap drop a
g a=1
gen N_1=1 if trained_MF_F!=. & adopt_seeds_MF_F!=.

local vars "one_tech_F trained_MF_F adopt_seedsY_MF_F adopt_seedsY_BRACnotMF_F adopt_seedsY_notBRAC_F adopt_seedsY_BRAC_F adopt_seedsY_F treatSpill_F treatSpillS_F"

collapse (sum) treated_F N_1 know*, by(agri_unique)

gen prop_adopt_trained=treated_F/N_1

g tieT=(know_onlyMF_B+know_onlynotMF_B)

gsort tieT know_onlyMF_B
gen n=_n
gen village_ID=_n

gen shareD=know_onlyMF_B/tieT
twoway (bar   tieT village_ID, fcolor(white) lcolor(black) horizontal ) (bar know_onlyMF_B village_ID  , horizontal fcolor(black) lcolor(black) )  , ytitle("Community ID", size(small)) ytitle("") ylab(0(3)60, labsize(vsmall))  legend( rows(3) label(2 "Number farmers tied exclusively to delivery agent") label(1 "Number farmers tied exclusively to counterfactual agent") ) graphregion(fcolor(white)) xtitle("Number of DA+CA ties")
gr_edit yaxis1.title.text.Arrpush Community ID

graph export "Fa1.pdf", replace



* Figure A2: Consumption and Agricultural Profits

******** Adopt tech 

use "SocialIncentives_Data_TandC.dta", clear
sum adopt_tech_ever_B, d
gen med_tech=1 if adopt_tech_ever_B>3 & adopt_tech_ever_B!=.
replace med_tech=0 if adopt_tech_ever_B<=3 & adopt_tech_ever_B!=.
local k=0
*forvalues i=0/1 {
	foreach y in netoutput2se_B {
local k=`k'+1
reg `y' `controls'
predict res_y`k' if `y'!=., res
}



sum consumption_food2se_B, d

npregress series res_y1 i.med_tech consumption_food2se_B, vce(bootstrap, reps(100) seed(111))

margins med_tech, at(consumption_food2se_B=(0(100)700)) vce(bootstrap, reps(100) seed(111))  

marginsplot,  recast(line) recastci(rarea)  plot1opts(lcolor(black) lpatter(longdash)) plot2opts(lcolor(black)) ///
ci1opts(lcolor(black) lpattern(longdash) fcolor(gs9%30)) ci2opts(lcolor(black) fcolor(gs1%30)) ///
legend(order(1 "HHs who ever adopted at least 3 techniques at baseline"  2 "HHs who ever adopted less than 3 techniques at baseline") cols(1)) ///
ytitle(Profits) graphregion(color(white)) xtitle("Food Expenditure") title("")

graph export "Fa2_A.pdf", replace

******** Adopted Seeds

use "SocialIncentives_Data_TandC.dta", clear
local k=0
replace adopt_seeds_ever_B=1-adopt_seeds_ever_B

foreach y in netoutput2se_B {
local k=`k'+1
reg `y' $controls 
predict res_y`k' if `y'!=., res
}


sum consumption_food2se_B, d

npregress series res_y1 i.adopt_seeds_ever_B consumption_food2se_B, vce(bootstrap, reps(100) seed(111))

margins adopt_seeds_ever_B, at(consumption_food2se_B=(0(100)300)) vce(bootstrap, reps(100) seed(111))  

marginsplot,  recast(line) recastci(rarea)  plot1opts(lcolor(black) lpattern(longdash)) plot2opts(lcolor(black)) ///
ci1opts(lcolor(black) fcolor(gs9%30) lpattern(longdash)) ci2opts(lcolor(black) fcolor(gs1%30)) legend(order(1 "HHs who ever adopted improved seeds at baseline"  2 "HHs who never adopted improved seeds at baseline") cols(1)) ///
ytitle(Profits) graphregion(color(white)) xtitle("Food consumption") title("")
graph export "Fa2_B.pdf", replace

